//<{$lang.search}>
var paginationTotal = 1;//记录总数
var isSearch = false;
function submitsearch() {
    isSearch = true;
    paginationCurrentPage = 1;
    initData(0);
}

function changrow(code){
   $("#r_"+code).remove();
   $('tr:[name=info_'+code+']').remove();
}

//<{$lang.packaging_material}><{$lang.list}>
function loadPackageSelect(warehouseId,customerId) {
    $.ajax({
        type:"post",
        async:false,
        dataType:"json",
        url:"/product/Package/get-available",
        data:{'warehouse_id':warehouseId,'customer_id':customerId},
        success:function (json) {
            var list = "";
            if (json.ask == 1) {
                $.each(json.result, function (key, val) {
                    list += "<option value=" + val.pp_id + ">" + val.pp_name + "</option>";
                });
            }
            $("#packet").html(list);
        }
    });
}
//<{$lang.edit}><{$lang.product}>
function loadProduct(qcocode) {
    $.ajax({
        type:"post",
        async:false,
        dataType:"json",
        url:"/product/receiving/get-product-info",
        data:{'qcocode':qcocode},
        success:function (json) {
            var list = "";
            if (json.ask) {
                $(".<{$lang.sku}>").html(json.result.product_sku);
                $("[name=productId]").val(json.result.product_id);
                $("[name=category]").val(json.result.category.poc_id);
                $("[name=weight]").val(json.result.product_weight);
                $("[name=old_weight]").val(json.result.product_weight);
                $("[name=declared]").val(json.result.product_declared_value);
                $("[name=product_length]").val(json.result.product_length);
                $("[name=product_width]").val(json.result.product_width);
                $("[name=product_height]").val(json.result.product_height);
                $('#packet').val(json.result.packet.package_id);
                $("[name=qco_code]","#editpform").val(qcocode);
				$("#pic").html('<a href="/product/product/view-product-info/productId/'+json.result.product_id+'" target="_blank">'+'<img height=\"200px\" width=\"200px\" src=\"/product/product/view-picture/id/'+json.result.product_id+'\"></a>'
				);
            }
        }
    });
}
$(function () {
	// 弹出<{$lang.tip}>
	$("#alert_msg").dialog({
		autoOpen : false,
		modal : true,
		width : 300,
		show : "slide",
		buttons : {
			'<{$lang.close}>' : function() {
				$(this).dialog('close');
			}
		}
	});
	
	$("#printDialog").dialog({
		autoOpen : false,
		modal : true,
		width : 500,
		show : "slide",
		buttons : {
			"OK" : function() {
				$("#qcPrintForm").submit();
				$(this).dialog("close");
			},
			"Close" : function() {
				$(this).dialog("close");
			}
		}
	});
	
	
    $(".submitsearch").click(function () {
        var qco_code = $("[name=qco_code]").val();
        var customer_id = $("[name=customer_id]").val();
        var dateFrom = $("[name=dateFrom]").val();
        var dateTo = $("[name=dateTo]").val();
        var receivingNo = $("[name=receivingNo]").val();
		var status = $("[name=status]").val();
        if (qco_code == '' && dateFrom == '' && dateTo == '' && status == '' && receivingNo=='' && customer_id=='') {
            alertTip('<{$lang.pls_input}><{$lang.search}>内容!');
            return;
        }
		if((dateFrom == '' && dateTo != '') || (dateFrom != '' && dateTo == '')){ alertTip('<{$lang.pls_input}>开始/结束<{$lang.date}>!'); return;}
        submitsearch();
    });
    
    <{if $lang.current_language=='cn'}>
    var dayNamesMin =['日','一', '二', '三', '四', '五', '六'];
    var monthNamesShort = ['01月','02月','03月','04月','05月','06月','07月','08月','09月','10月','11月','12月'];
    $('#dateFrom,#dateTo').datepicker({
        dayNamesMin:dayNamesMin,
        monthNamesShort:monthNamesShort,
        changeMonth:true,
        changeYear:true,
        dateFormat:'yy-mm-dd'
    });
	<{else}>]
	 $('#dateFrom,#dateTo').datepicker({
	        changeMonth:true,
	        changeYear:true,
	        dateFormat:'yy-mm-dd'
	    });
	<{/if}>
	$(".checkAllRecord").live("click", function() {
		if (typeof $(".checkAllRecord").attr("checked") == "string") {
			$(".checkRecordProduct").attr("checked", true);
		} else {
			$(".checkRecordProduct").attr("checked", false);
		}
	});
	$('.toggleButton').click(function(){
		if($(this).attr("status") != 1){
			$(".detailInfo").hide();
			$(this).attr("status",1);
			$(this).val("<{$lang.folder}>");
		}else{
			$(".detailInfo").show();
			$(this).attr("status",0);
			$(this).val("<{$lang.unfolder}>");
		}
	});
	
//	submitsearch();
});
//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>" class="alertTip"><p align="center">' + tip + '</p></div>').dialog({
        modal:true,
        width:450,
        buttons:{
            '<{$lang.ok}>':function () {
                $(this).dialog("close");
            }
        }
    });
}

function loadData(page, pageSize) {
    var vForm = "";
    if (isSearch == true && isSearch != undefined) {
        vForm = $("#sForm").serializeArray();
    }
    $.ajax({
        type:"POST",
        async:false,
        dataType:"json",
        url:"/product/qc/index/page/" + page + "/pageSize/" + pageSize,
        data:vForm,
        success:function (json) {
            var html = "";
            var module = '';
            paginationTotal = json.total;
            if(page==0){page=1}
            var i = (page == 1 ? 1 : pageSize * (page - 1) + 1);
			var css = $(".toggleButton").attr("status")==0?"":"display:none;";
            if (json.ask == 0) {
                html = "<tr><td colspan='14' align='center'><{$lang.no_data}></td></tr>";
            } else {
                $.each(json.result, function (k, v) {
                    var user_id = (v.user_id == null) ? '' : v.user_id;
				    var status = v.qco_status;
					if(v.is_qc==1){
					   html += '<tr id="r_'+v.qco_code+'" class="qcInfo">';
					}else{
					   if(v.order_type==4){
					      html += '<tr id="r_'+v.qco_code+'" style="color:red;">';
					   }else{
					      html += '<tr id="r_'+v.qco_code+'" style="color:blue;">';
					   }
					   
					}
                    
					
					html += '<td><input type="checkbox" class="checkRecordProduct" name="checkRecordProduct[]" value="'+v.qco_code+'"></td>';
                    html += '<td>' + (i++) + '</td>';
					html += '<td>' + (v.ri_sort == 1 ? '<{$lang.priority_priority}>' : '<{$lang.normal}>') + '</td>';
                    html += '<td class="showDetail toggleCode" qcoId="'+v.qco_id+'">'+v.qco_code+'</td>';
                    html += '<td class="showDetail toggleCode" qcoId="'+v.qco_id+'">' + v.qco_ref_code + '</td>';
                    html += '<td>' + v.customer_id + '</td>';
                    html += '<td>' + v.qco_quantity + '</td>';
					html += "<td>" + v.qco_real_quantity + "</td>";
                    html += "<td>" + v.qco_quantity_pass + "</td>";
                    html += "<td>" + v.statusText+ "</td>";
                    html += "<td>" + user_id + "</td>";
                    
                    html += "<td>" + v.contact + "<br />"  + v.contact_mobile + "</td>";				//联系方式                    
                    html += "<td><{$lang.create}>:" + v.qco_create_time + "<br /><{$lang.update}>:" + v.qco_last_update + "</td>";	// <{$lang.date}>
                                        
                    html += "<td>";
					if(status == 0){
						html += '<img src="/images/icon/printer.gif" title="<{$lang.print}>" style="cursor:pointer" qco_code="' + v.qco_code + '" class="print_detail"/>';
						html += '<img src="/images/icon/edit.gif" title="<{$lang.quanlity}>" style="cursor:pointer" qco_code="' + v.qco_code + '" class="quality_control"/>';
					}else if(status == 1){
						html += '<img src="/images/icon/edit.gif" title="<{$lang.quanlity}>" style="cursor:pointer" qco_code="' + v.qco_code + '" class="quality_control"/>';
					}else if(status == 2){
						html += '<img src="/images/icon/icon_view.gif" title="<{$lang.detail}>" style="cursor:pointer" qco_code="' + v.qco_code + '" class="print_detail"/>';
					}
                	
                    html += "</td>";
                    html += "</tr>";
					
					// 每次 的<{$lang.product}>具体<{$lang.msg}>
					html += '<tr id="detailInfo'+v.qco_id+'" class="detailInfo" name="info_'+v.qco_code+'" style="'+css+'font-style:italic;background:#D7EBF6;">';
					html += '<td colspan="14">';
					html += '<table width="100%"><tr style="font-weight:bold;">';

					html += '<td width="10%"><{$lang.sku}></td>';
					html += '<td width="10%"><{$lang.picture}></td>';
					html += '<td width="40%"><{$lang.sku}><{$lang.name}></td>';
					html += '<td width="18%"><{$lang.category}></td>';
					html += '<td width="6%"><{$lang.weight}></td>';
					html += '<td width="8%"><{$lang.quanlity}><{$lang.quantity}></td>';					
					html += '<td width="8%"><{$lang.suitability}><{$lang.quantity}></td>';

					html += '</tr>';
					
					$.each(v.qciPros,function(pK,pV){
						html += '<tr>';
						html += '<td><a href="/product/product/view-product-info/productId/'+pV.product_id+'" target="_blank">'+v.customer_id+'-'+pV.<{$lang.sku}>No+'</a></td>';
						
						html += '<td>'+ '<div><img src="/product/product/view-picture/id/'+pV.product_id+'\" width=75px height=75px></div>'+'</td>';
						
						html += '<td>'+pV.<{$lang.sku}>Name+'</td>';
						html += '<td>'+pV.category+'</td>';
						html += '<td>'+pV.weight+'</td>';
						html += '<td>'+pV.qci_quantity+'</td>';
						html += '<td>'+pV.qci_quantity_pass+'</td>';
						html += '</tr>';
					});
					html += '</table></td></tr>';
                });
				
            }
            $("#listloadData").html(html);
			$(".toggleCode").click(function(){
				$("#detailInfo"+$(this).attr("qcoId")).toggle();
			});
            $(".qcInfo:even").addClass('even-tr');
        }
    });
}
//<{$lang.product}><{$lang.edit}>
$(function(){
    $("#editDialog").dialog({
        autoOpen:false,
        modal:true,
        show:"slide",
        width:500,
        buttons:{
            "<{$lang.ok}>":function(){
			   
                postEditPorudct();
            },
            "<{$lang.cancel}>":function(){
                $(this).dialog("close");
            }
        }
    });
});
$(".quality_control").live("click",function(){
    var obj = this;
    $.ajax({
        url:"/product/receiving/check-has-first/qcocode/" + $(this).attr("qco_code"),
        dataType:"json",
        success:function (json) {
            if (json.ask) {
                $("#alert_msg").dialog("option", "buttons", [
                    {
                        text:"<{$lang.yes}>",
                        click:function () {
                            loadPackageSelect(json.result.warehouse_id,json.result.customer_id);
                            loadProduct($(obj).attr("qco_code"));
                            //window.open("/product/qc/operation/code/"+$(obj).attr("qco_code"));
                            $(this).dialog("close");
                            $('#editDialog').dialog('open');
                        }
                    },
                    {
                        text:"<{$lang.no}>",
                        click:function () {
                            $(this).dialog("close");
                        }
                    }
                ]).html("<{$lang.first}><{$lang.receive}>?").dialog("open");

            } else {
                window.open("/product/qc/operation/code/"+$(obj).attr("qco_code"));
            }
        }
    });

});

$(".print_detail").live("click",function(){
    window.open("/product/qc/operation/code/"+$(this).attr("qco_code"));
	//window.open("/product/qc/detail/code/"+$(this).attr("qco_code"));
});

function postEditPorudct(){
    var data = $("#editpform").serializeArray();
    var qcocode=$("[name='qco_code']","#editpform").val();
    $.ajax({
        url:"/product/receiving/edit-product",
        type:"post",
        dataType:"json",
        async:false,
        data:data,
        success:function(json){
            if(json.ask){
                if(qcocode!=''){
                    $("#editDialog").dialog("close");
                    window.open("/product/qc/operation/code/"+qcocode);
                }else{
                    location.reload();
                }
            }else{
                alertTip(json.message);
            }
        }
    });
}

function qcPrintProduct(printType) {
	if ($(".checkRecordProduct:checked").size()) {
		
		var qcoidStr = '';
		$(".checkRecordProduct:checked").each(function() {
			qcoidStr += this.value + ',';
		});
		loading();
		$.post('/product/qc/get-qc-product',{'qcoidStr':qcoidStr},function(json){
			var html = "<form action='qc-print-label' method='post' id='qcPrintForm' target='_blank'>";
			html += '<input type="hidden" name="type" value="'+printType+'">';
			html += '<table class="myTab" width="100%" cellspacing=3 cellpadding=3  border=1>';
			html += '<tr >';
			html += '<td ><{$lang.sku}> Name</td>';
			html += '<td ><{$lang.sku}></td>';
			html += '<td >Category</td>';
			html += '<td class="no-background">Qty</td>';
			html += '</tr>';
			$.each(json,function(k,v){
				$.each(v.product,function(i,val){
					html += '<tr>';
					html += '<td>'+val.<{$lang.sku}>Name+'</td>';
					html += '<td>'+(val.customer_id + '-' +val.<{$lang.sku}>No)+'</td>';
					html += '<td>'+val.category+'</td>';
					html += '<td><input type="text" class="width1" value="'+val.order_qty+'" size=3 name="count'+val.product_id+'"></td>';
					html += '</tr>';
				});
			});	
			html += '</table>';
			html += "</form>";
			$("#printDialog").html(html).dialog("open");
			closeLoading();
		},'json');
		
	} else {
		$('#alert_msg p').text("<{$lang.pls}><{$lang.log}>");
		$('#alert_msg').dialog('open');
	}
}

